<?php

namespace app\admin\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;



class Protoco extends Base
{
    public  $model;

    public function __construct()
    {
   $this->model = model("Protocol");
        parent::__construct();

    }

    /**
     * @return \think\response\Json  询价历史
     */
    public function index()
    {//
        //搜索条件 时间 用户号码
        $where = [];
        //时间的处理
      if($this->agents_ids=="all"){
          //超级管理员
          $res = $this->model->Protoco_admin_page($where);
      }else{
          //代理商
          $res = $this->model->Protoco_page($where);
      }


     return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }

    public function index2()
    {//
        //搜索条件 时间 用户号码
        $where = [];

        //时间的处理

        if($this->agents_ids=="all"){
            //超级管理员
            $res = $this->model->Protoco_admin_page2($where);
        }else{
            //代理商
            $res = $this->model->Protoco_page2($where);
        }

        return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }


    //保存用户信息
    public function save(){


        $data = [];
        $file = request()->file('image');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $uploadPath = ROOT_PATH . 'public' . DS . 'uploads/';
            $info = $file->move($uploadPath);
            if($info){
                 $data['image'] = $uploadPath.$info->getSaveName();

            }else{
                // 上传失败获取错误信息
                    return failJson("上传失败");
            }


        }
        $data['title'] = $this->title;
        $data['content'] = htmlspecialchars($this->content);
        $data['type'] = $this->type;
        $data['is_position'] = $this->is_position;
    /*    if($this->sidRows['agent_id']){
            $data['agent_id'] = $this->agent_id;
        }*/

        $res = $this->model->save($data);
        if($res){
            return     sucessJson("添加成功");
        }else{
            return      failJson("添加失败");
        }

    }


    public function updateit(){


        $data = [];
        $ressult = $this->model->where("id",$this->id)->find();
        if($ressult['agent_id']!=$this->sidRows['agent_id']){
            failJson("你暂无权限更新");
        }
        if(isset($this->image)){
            $file = request()->file('image');

            // 移动到框架应用根目录/public/uploads/ 目录下
            if($file){
                $uploadPath = ROOT_PATH . 'public' . DS . 'uploads/';
                $info = $file->move($uploadPath);
                if($info){
                    $data['image'] = $uploadPath.$info->getSaveName();

                }
            }
        }

        $data['title'] = $this->title;
        $data['content'] = htmlspecialchars($this->content);
        $data['type'] = $this->type;
        $data['is_position'] = $this->is_position;


        $res = $this->model->save($data,['id'=>$this->id]);

        if($res){
         return    sucessJson("更新成功");
        }else{
            return   failJson("更新失败");
        }
    }




    public function del(){
              $res =   $this->model->delete(['id'=>$this->id]);
        if($res){
            return   sucessJson("删除成功");
        }else{
            return     failJson("删除失败");
        }
    }




}
